package org.javaongems.client.vcl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.javaongems.client.forms.CustomForm;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

public class MountPane extends Container {
	final static private String BODY = "<<body>>"; 
	final static private Map mountPoints = new HashMap(); 
		  static private MountPane bodyMount;
		  
		  		 private ArrayList forms = new ArrayList(); 
	
	static public MountPane get(String domIdToMountOn) {
		if (domIdToMountOn == null)
			domIdToMountOn = BODY;
		MountPane ret = (MountPane) mountPoints.get(domIdToMountOn);
		if (ret == null) {
			ret = new MountPane();
			if (BODY.equals(domIdToMountOn))
				bodyMount = ret;
			ret.mount(domIdToMountOn);
			if (ret.getElement() == null)
				ret = null;
			else 
				mountPoints.put(domIdToMountOn, ret);
		}
		if (ret == null)
			throw new IllegalArgumentException("invalid DOM id [" + domIdToMountOn + "] to mount on");
		return ret;
	}
	
	public List getForms() {
		return forms;
	}
	
	static public void loadGemsContainer(String descriptor, Map objectSpace, Container targ) {
		JsonComponentReader rdr = new JsonComponentReader();
		rdr.readRootContainer(descriptor, objectSpace, targ);
	}
	
	protected void mount(String domId) {
		setName(domId);
		initialiseDomElement();
	}
	
	public boolean setElement(Element elm) {
		boolean ret = super.setElement(elm);
		if (ret)
			domRenderer.setPositionStyle(ControlDomRenderer.POSITION_RELATIVE);
		return ret;
	}
	
	protected Element createElement() {
		String name = getName();
		Element ret;
		if (BODY.equals(name))
			ret = getBodyElement();
		else
			ret = DOM.getElementById(getName());
		return ret;
	}

	public static native Element getBodyElement() /*-{
		return $doc.body;
	}-*/;
	
	protected void adopt(Control child) {
		super.adopt(child);
		child.invalidate();
		if (child instanceof CustomForm) {
			forms.add(child);
		}
	}
	
	protected void disown(Control child) {
		super.disown(child);
		if (child instanceof CustomForm) 
			forms.remove(child);
	}
	
	protected boolean paintFrame() {
//		if (this != bodyMount)
//			super.paintFrame();
		// i dont think you need to paint the frame at all
		return false;
	}
	
	protected void paintChildren() {
		if (this == bodyMount)
			paintChildrenZorder();
		else
			super.paintChildren();
	}
	
	protected void paintAttributeAlignment() {
		// no alignment on the mount pane
	}
	
	protected boolean isCanvasReady() {
		return true;
	}
	
	protected ControlDomRenderer createDomRender() {
		return new MountControlDomRenderer();
	}
	
	static protected class MountControlDomRenderer extends ControlDomRenderer {
		public boolean setElement(Element elm) {
			boolean ret = super.setElement(elm);
			if (ret && control == bodyMount)
				DOM.setEventListener(element, control);
			return ret;
		}
		
		public void resetElement() {
			// dont want to be able to reset the mount
		}
	}
}
